package com.heima.article.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.heima.apis.article.IArticleClient;
import com.heima.article.service.IApArticleConfigService;
import com.heima.article.service.IApArticleService;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.vos.ArticleCommnetVo;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.vos.PageResultVo;
import com.heima.model.wemedia.dtos.StatisticsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/v1/article")
public class ArticleClient implements IArticleClient {

    @Autowired
    private IApArticleService apArticleService;
    @Autowired
    private IApArticleConfigService apArticleConfigService;

    @Override
    @PostMapping("/save")
    public ResponseResult<Long> saveArticle(@RequestBody ArticleDto dto) {
        return apArticleService.saveArticle(dto);
    }

    /**
     * 删除文章中引用的图片
     *
     * @param url 图片Id
     */
    @Override
    @DeleteMapping("/images")
    public ResponseResult<String> markImageAsDeleted(@RequestParam String url) throws JsonProcessingException {
        // 将图片替换为deleted.png
        return apArticleService.markImageAsDeleted(url);
    }

    /**
     * 删除文章
     *
     * @param id 文章id
     */
    @Override
    @DeleteMapping("/{id}")
    public void delArticleById(@PathVariable Long id) {
        apArticleService.delArticleById(id);
    }

    @GetMapping("/queryLikesAndConllections")
    @Override
    public ResponseResult<Map<String, Object>> queryLikesAndConllections(
            @RequestParam("wmUserId") Integer wmUserId,
            @RequestParam("beginDate") Date beginDate,
            @RequestParam("endDate") Date endDate) {
        return apArticleService.queryLikesAndConllections(wmUserId, beginDate, endDate);
    }

    @PostMapping("/newPage")
    @Override
    public PageResultVo<ApArticle> newPage(StatisticsDto dto) {
        return apArticleService.newPage(dto);
    }

    @GetMapping("/findArticleConfigByArticleId/{articleId}")
    @Override
    public ResponseResult<ApArticleConfig> findArticleConfigByArticleId(@PathVariable Long articleId) {
        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(
                Wrappers.<ApArticleConfig>lambdaQuery()
                        .eq(ApArticleConfig::getArticleId, articleId));
        return ResponseResult.okResult(apArticleConfig);
    }

    @PostMapping("/findNewsComments")
    @Override
    public PageResponseResult<List<ArticleCommnetVo>> findNewsComments(@RequestBody ArticleCommentDto dto) {
        return apArticleService.findNewsComments(dto);
    }

    @PostMapping("/updateCommentStatus")
    @Override
    public ResponseResult<AppHttpCodeEnum> updateCommentStatus(@RequestBody CommentConfigDto dto) {
        return apArticleConfigService.updateCommentStatus(dto);
    }
}